﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Text;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;

namespace Recon.Portal.WebUI
{
    public partial class AuthorizeRemitter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetUnauthorizedRemitterList();
            }
        }

        protected void btnAuthorize_Click(object sender, EventArgs e)
        {
            AuthorizeRemitters();
        }

        protected void grdAuthList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdAuthList.PageIndex = e.NewPageIndex;
            GetUnauthorizedRemitterList();
        }

        protected void grdAuthList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowIndex > -1)
            //{
            //    DateTime dt;
            //    if (e.Row.Cells[2].Text != "" && e.Row.Cells[2].Text != "&nbsp;")
            //    {
            //        dt = Convert.ToDateTime(e.Row.Cells[2].Text);
            //        e.Row.Cells[2].Text = dt.ToString("dd-MM-yyyy");
            //        e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Center;
            //    }
            //    else
            //    {
            //        e.Row.Cells[2].Text = "";
            //    }
            //    if (e.Row.Cells[6].Text != "" && e.Row.Cells[6].Text != "&nbsp;")
            //    {
            //        dt = Convert.ToDateTime(e.Row.Cells[6].Text);
            //        e.Row.Cells[6].Text = dt.ToString("dd-MM-yyyy");
            //        e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Center;
            //    }
            //    else
            //    {
            //        e.Row.Cells[6].Text = "";
            //    }

            //    double value = 0.00;
            //    if (e.Row.Cells[4].Text != "" && e.Row.Cells[4].Text != "&nbsp;")
            //    {
            //        value = Convert.ToDouble(e.Row.Cells[4].Text);
            //        e.Row.Cells[4].Text = value.ToString("#,#.00", CultureInfo.InvariantCulture);
            //        e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            //    }
            //    else
            //    {
            //        e.Row.Cells[4].Text = "0.00";
            //    }
            //    if (e.Row.Cells[8].Text != "" && e.Row.Cells[8].Text != "&nbsp;")
            //    {
            //        value = Convert.ToDouble(e.Row.Cells[8].Text);
            //        e.Row.Cells[8].Text = value.ToString("#,#.00", CultureInfo.InvariantCulture);
            //        e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Right;
            //    }
            //    else
            //    {
            //        e.Row.Cells[8].Text = "0.00";
            //    }

            //}
        }

        private void GetUnauthorizedRemitterList()
        {
            try
            {
                List<Remitter> dtAuthList = (new RemitterEntity()).GetUnauthorizedRemitters(((User)Session["CurrentUserVO"]).UserID);
                grdAuthList.DataSource = dtAuthList;
                grdAuthList.DataBind();
            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    throw;
                }
            }
        }

        private void AuthorizeRemitters()
        {
            try
            {
                int count;
                count = 0;
                int selectedCheckBoxCount;
                selectedCheckBoxCount = 0;
                StringBuilder checkedList = new StringBuilder();
                foreach (System.Web.UI.WebControls.GridViewRow dgi in grdAuthList.Rows)
                {

                    CheckBox chkSelect = (CheckBox)dgi.Cells[0].FindControl("chkSelect");
                    if (chkSelect.Checked)
                    {
                        checkedList.Append("'" + dgi.Cells[1].Text.ToString() + "'" + ",");
                        selectedCheckBoxCount++;
                    }

                }

                if (selectedCheckBoxCount == 0)
                {
                    lblMessage.Text = "Please select a remitter to Authorize!";
                    btnClickPopup.Show();
                }
                else
                 { 
                        checkedList.Remove(checkedList.Length - 1, 1);

                        count = (new RemitterEntity()).AuthorizeRemitters(((User)Session["CurrentUserVO"]).UserID, checkedList.ToString());
                 }
                if (count == 0)
                {
                    lblMessage.Text = "Please select a remitter to Authorize!";
                    btnClickPopup.Show();
                }
                else
                {
                    lblMessage.Text = "Successfully Authorised (" + count + ") Remitter!";
                    btnClickPopup.Show();
                }

                GetUnauthorizedRemitterList();

            }
            catch (Exception ex)
            {
                if (ex != null)
                {
                    throw;
                }
            }
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }
    }
}